import { get, post, put, del } from '@/utils/request';
import type { Role, PaginatedResponse } from '@/types';
import type { GetRolesParams, CreateRoleData, UpdateRoleData, AssignPermissionsData } from './type';

/**
 * 角色相关API
 */

// 获取角色列表
export const getRoles = (params?: GetRolesParams) => {
  return get<PaginatedResponse<Role>>('/roles', params);
};

// 获取角色详情
export const getRoleById = (id: string) => {
  return get<Role>(`/roles/${id}`);
};

// 创建角色
export const createRole = (data: CreateRoleData) => {
  return post<Role>('/roles', data);
};

// 更新角色
export const updateRole = (data: UpdateRoleData) => {
  const { id, ...rest } = data;
  return put<Role>(`/roles/${id}`, rest);
};

// 删除角色
export const deleteRole = (id: string) => {
  return del(`/roles/${id}`);
};

// 获取角色的权限
export const getRolePermissions = (roleId: string) => {
  return get<string[]>(`/roles/${roleId}/permissions`);
};

// 分配权限
export const assignPermissions = (data: AssignPermissionsData) => {
  const { roleId, permissionIds } = data;
  return post(`/roles/${roleId}/permissions`, { permissionIds });
};

